// pages/Detailspage/Detailspage.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assets:app.config.assets,
    detail:{},
    id:0,
    favorite:0,
    userinfo:{},
    platform:"devtool"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    this.setData({
      id: options.id
    })
    var context = this;
    wx.getSystemInfo({
      complete: (res) => {
        console.log(res);
        context.setData({
          platform: res.platform
        })
      },
    })
    this.getData();
  },

  getData: function(){
    var context = this;
    wx.setStorageSync('course', {})
    wx.request({
      url: app.config.server + '/api/userinfo',
      header:{
        sessionkey:wx.getStorageSync('session_key')
      },
      success: res => {
        console.log(res);
        if(res.data.name == 'null'){
          res.data.name = '';
        }
        context.setData({
          userinfo:res.data
        })
      }
    })
    
    wx.request({
      url: app.config.server + "/api/course/"+this.data.id,
      success:res => {
        console.log(res);
        context.setData({
          detail:res.data
        })
      }
    })

    wx.request({
      url: app.config.server + '/api/favorites?course_id='+this.data.id,
      header:{
        sessionkey:wx.getStorageSync('session_key')
      },
      success:res => {
        console.log(res);
        context.setData({
          favorite:res.data.length
        })
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "智原学" + this.data.detail.title,
      desc: this.data.detail.description,
      path: '/pages/Detailspage/Detailspage?id=' + this.data.detail.id + '&share_openid=' + wx.getStorageSync('openid')
    }
  },
  tapBuy:function(e){
    var context = this;
    if((this.data.userinfo.name == '' || this.data.userinfo.name ==undefined) || (this.data.userinfo.telephone == '' || this.data.userinfo.telephone == undefined)){
      wx.showModal({
        title: '提示',
        content: '为确保您的权益，请您填写真实姓名和手机号码，是否立即填写?',
        success (res) {
        if (res.confirm) {
          wx.setStorageSync('course', context.data.detail);
          wx.redirectTo({
            url: '/pages/personal/personal',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
        }
      });
      return false;
    }
    console.log(e);
    wx.request({
      url: app.config.server + '/api/orders/create',
      method:'POST',
      header:{
        sessionkey:wx.getStorageSync('session_key')
      },
      data:{
        courses:[
          {
            course_id:e.currentTarget.dataset.id,
            price:e.currentTarget.dataset.price,
            quantity:1,
            title:e.currentTarget.dataset.title,
          }
        ]
      },
      success:res => {
        console.log(res);
        var order_id = res.data.order_id;
        wx.requestPayment({
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          paySign: res.data.paySign,
          timeStamp: res.data.timeStamp + "",
          signType:"MD5",
          success: res => {
            console.log(res);
            wx.request({
              url: app.config.server + '/api/orders/update',
              method:"POST",
              header:{
                sessionkey: wx.getStorageSync('session_key')
              },
              data:{
                order_id:order_id,
                status:20
              },
              success: res => {
              },
              complete: res => {
                wx.redirectTo({
                  url: '/pages/myorder/myorder',
                  complete: (res) => {},
                  fail: (res) => {},
                  success: (res) => {},
                })
              }
            })
          },
          complete: res => {
            console.log(res);
          }
        })
      }
    })
  },
  tapcollection:function(e){
    var course_id = this.data.detail.id;
    if(e.currentTarget.dataset.favorite){
      wx.request({
        url: app.config.server + '/api/favorite/delete',
        header:{
          sessionkey:wx.getStorageSync('session_key')
        },
        data:{
          course_id:course_id
        },
        method:'POST',
        success:res => {
          this.setData({
            favorite:0
          })
        }
      })
    }else{
      wx.request({
        url: app.config.server + '/api/favorite/create',
        header:{
          sessionkey:wx.getStorageSync('session_key')
        },
        data:{
          course_id:course_id
        },
        method:'POST',
        success:res => {
          this.setData({
            favorite:1
          })
        }
      })
    }
  }
})